#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/12/12 18:30
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 高级-生命游戏.py
# @Software: PyCharm
from utils.main import main, opt_list


class Solution:
    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        op = [
            (0, 1),
            (1, 1),
            (1, 0),
            (1, -1),
            (0, -1),
            (-1, -1),
            (-1, 0),
            (-1, 1)
        ]

        n0 = len(board)
        if n0 <= 0:
            return
        n1 = len(board[0])
        for x in range(n0):
            for y in range(n1):
                p = 0
                for i in range(8):
                    xx = x + op[i][0]
                    yy = y + op[i][1]
                    if 0 <= xx < n0 and 0 <= yy < n1:
                        p += board[xx][yy] % 10
                board[x][y] += p * 10

        for x in range(n0):
            for y in range(n1):
                p = board[x][y] // 10
                old = board[x][y] % 10
                if old == 1:
                    if p < 2:
                        board[x][y] = 0
                    elif p < 4:
                        board[x][y] = 1
                    else:
                        board[x][y] = 0
                else:
                    if p == 3:
                        board[x][y] = 1
                    else:
                        board[x][y] = 0


def opt(board):
    solve = Solution()
    print(board)
    print('ans:')
    print(solve.gameOfLife(board))
    print(board)


def opt1():
    board = [
        [0, 1, 0],
        [0, 0, 1],
        [1, 1, 1],
        [0, 0, 0]
    ]
    opt(board)


if __name__ == '__main__':
    opt_list.append(opt1)
    main()
